app.controller('mynews', function ($scope, $http) {
    var token = localStorage.getItem('token');
    $scope.newsInfo = [];

    $scope.showNews = function () {
        if (!token) {
            $.toast("请先登录");
            location.href = '#user-login';

            return;
        }
        $.showPreloader('请稍候');
        if (token) {

            $http({
                method: 'get',
                url: app.netPath + '/interaction/show/mynews',
                headers: {Authorization: token}
            }).then(function (res) {
                $.hidePreloader();
                if (!res.data.error) {
                    if(res.data.data==undefined){
                        $scope.listSize=0
                    }
                    console.log(res.data.data);
                    $scope.newsInfo = res.data.data;
                    $scope.len = res.data.data.length;

                    //回复者头像
                    for (var i = 0; i < $scope.newsInfo.length; i++) {
                        if ($scope.newsInfo[i].user.userAvatarAddress) {
                            $scope.newsInfo[i].user.userAvatarAddress = app.netPath + '/resources/img/' + $scope.newsInfo[i].user.userAvatarAddress;
                            app.buildImageUrl($scope.newsInfo[i].user,'userAvatarAddress',function () {
                                $scope.$digest();
                            });
                        }
                    }
                    //动态图片
                    for (var i = 0; i < $scope.newsInfo.length; i++) {
                        if ($scope.newsInfo[i].dynamic.dynamicImg) {
                            $scope.newsInfo[i].dynamic.dynamicImg = app.netPath + '/resources/dynamicImg/' + $scope.getDynamicImg($scope.newsInfo[i].dynamic.dynamicImg);
                            app.buildImageUrl($scope.newsInfo[i].dynamic,'dynamicImg',function () {
                                $scope.$digest();
                            });
                        }
                    }

                }
                else {
                    $.toast(res.data.msg);
                }
            },function(){
                $.hidePreloader();
                $.toast('网络走丢了！')
            })
        }
    };


    //动态图片分割（","）
    $scope.getDynamicImg = function (dynamicImgString) {
        var dynamicImg = dynamicImgString.split(',');
        return dynamicImg[0];
    };

    //忽略一条消息
    $scope.ignoreAReply = function (interactionId) {
        $.confirm("是否忽略此消息？",function(){
            $http({
                method: 'get',
                url: app.netPath + '/interaction/read/amynews',
                params: {interactionId: interactionId},
                headers: {Authorization: token}
            }).then(function (res) {
                if (!res.data.error) {
                    console.log(res.data.msg);
                    location.href = '#mynews'
                }
                else {
                    $.toast(res.data.msg);
                }
            })
        },function(){
            $.toast('忽略取消！')
        })
    };

    //忽略全部消息
    $scope.ignoreAllReply = function () {
        $.confirm('是否忽略全部消息？',function(){
            if ($scope.len != 0) {
                $http({
                    method: 'get',
                    url: app.netPath + '/interaction/read/listmynews',
                    headers: {Authorization: token}
                }).then(function (res) {
                    if (!res.data.error) {
                        console.log(res.data.msg);
                        location.href = '#mynews'
                    }
                    else {
                        $.toast(res.data.msg);
                    }
                })
            }
        },function(){
            $.toast('忽略取消！')
        })
    };

    //跳转到动态详情，得到一个dynamicId

    $scope.getDynamicId = function (dynamicId) {
        sessionStorage.setItem('dynamicId', dynamicId);
        location.href = '#evaluate-details'
    };

    //回复消息
    $scope.getInteraction = function (interactionId) {
        sessionStorage.setItem('interactionId', interactionId);
        location.href = '#evaluate-more'
    }

});